package com.ruoyi.bizsys.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.bizsys.domain.AppDeviceInfo;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.service.IAppDeviceInfoService;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.ruoyi.common.enums.UserTagEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bizsys.mapper.AppUserMarkMapper;
import com.ruoyi.bizsys.domain.AppUserMark;
import com.ruoyi.bizsys.service.IAppUserMarkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author african-loan
 * @date 2022-02-17
 */
@Slf4j
@Service
public class AppUserMarkServiceImpl extends ServiceImpl<AppUserMarkMapper, AppUserMark> implements IAppUserMarkService {

    @Autowired
    private IAppDeviceInfoService appDeviceInfoService;

    @Autowired
    private ICustomerInfoService customerInfoService;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public AppUserMark selectAppUserMarkById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param appUserMark 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AppUserMark> selectAppUserMarkList(AppUserMark appUserMark) {
        return this.list(new QueryWrapper<>(appUserMark).last("order by id desc"));
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param appUserMark 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAppUserMark(AppUserMark appUserMark) {
        appUserMark.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(appUserMark);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param appUserMark 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAppUserMark(AppUserMark appUserMark) {
        appUserMark.setUpdateTime(DateUtils.getNowDate());
        //根据用户ID 查询 用户等级，用户版本
        extracted(appUserMark);
        return baseMapper.updateById(appUserMark);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteAppUserMarkByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteAppUserMarkById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public void apdUserTag(AppUserMark userMark, UserTagEnum tagEnum) {
        log.info("---- UserTagEnum :" + tagEnum);
        userMark.setFirstTag(tagEnum.getFirstTag());
        userMark.setSecondTag(Optional.ofNullable(tagEnum.getSecondTag()).orElse(""));
        //根据用户ID 查询 用户等级，用户版本
        extracted(userMark);

        QueryWrapper<AppUserMark> wrapper = new QueryWrapper<AppUserMark>();
        if(userMark.getUserId().isEmpty()){
            wrapper.eq("register_phone", userMark.getRegisterPhone());
        }else{
            wrapper.eq("user_id", userMark.getUserId());
        }

        //如果是注册 用手机号码查找
        if (UserTagEnum.REGISTERED_NOT_PASSWORD.getFirstTag().equals(tagEnum.getFirstTag()) && UserTagEnum.REGISTERED_NOT_PASSWORD.getSecondTag().equals(tagEnum.getSecondTag())) {
            wrapper = new QueryWrapper<AppUserMark>().eq("register_phone", userMark.getRegisterPhone());
        }
        wrapper.last("limit 1");
        AppUserMark appUserMark = this.getOne(wrapper);
        //不存在则新增 存在则更新
        if (Func.isEmpty(appUserMark)) {
            this.insertAppUserMark(userMark);
        }else{
            log.info("---- appUserMark :" + appUserMark);
            userMark.setUpdateTime(new Date());
            this.update(userMark, wrapper);
        }
    }

    private void extracted(AppUserMark userMark) {
        QueryWrapper<AppDeviceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userMark.getUserId());
        List<AppDeviceInfo> AppDeviceInfo = appDeviceInfoService.list(queryWrapper);
        userMark.setAppVersion(AppDeviceInfo.size() >0 ? AppDeviceInfo.get(0).getAppVersion():null);

        QueryWrapper<CustomerInfo> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("user_id",userMark.getUserId());
        List<CustomerInfo> customerInfo = customerInfoService.list(customerQueryWrapper);
        userMark.setCreditRating(customerInfo.size() > 0?customerInfo.get(0).getCreditRating():null);
    }

    @Override
    public Integer insertBatch(List<AppUserMark> appUserMarkList) {
        if(appUserMarkList.size()>0){
            return baseMapper.insertBatchSomeColumn(appUserMarkList);
        }
        return 0;
    }

    @Override
    public void updateBatchByList(List<AppUserMark> appUserMarkList) {
        List<AppUserMark> appUserMarks = new ArrayList<>();
        for (int i = 0; i < appUserMarkList.size(); i++) {
            appUserMarks.add(appUserMarkList.get(i));
            if ((i + 1) % 1000 == 0||i == appUserMarkList.size() - 1) {
                baseMapper.updateBatchByList(appUserMarks);
                appUserMarks = new ArrayList<AppUserMark>();
            }
        }
    }


}
